package br.com.fa7.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletContext;

import org.directwebremoting.ServerContext;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;

import br.com.fa7.service.ManagementFight;

public class EventController {

	private int count = 0;
	WebContext webContext = WebContextFactory.get();
	ServletContext servletContext = webContext.getServletContext();
	ServerContext serverContext = ServerContextFactory.get(servletContext);
	ManagementFight managementFight = new ManagementFight();
	Thread treadFight = new Thread(managementFight);
	

	/**
	 * This method continually calls the update method utill the for loop
	 * completes
	 */
	@SuppressWarnings("rawtypes")
	public void getNewEvent() {
		treadFight.start();
		try {
			while(true){
				if(managementFight.hasNewEvent()){
					List<Data> messages = new ArrayList<Data>();
					messages.add(new Data(managementFight.getCurrentFightStatus()));
					Collection sessions = serverContext
							.getScriptSessionsByPage("/TCCComet/index.jsp");
					Util utilAll = new Util(sessions);
					utilAll.addOptions("updates", messages, "value");
				}
				
			}
		} catch (Exception e) {
			System.out.println("Error in Update");
			e.printStackTrace();
		}
	}

	
}
